package cn.net.mugui.net.pc.task;

import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;

import lombok.Getter;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

@Component
public class TMWsHandle extends WebSocketListener {
	@Getter
	private WebSocket socket = null;

	@Autowired
	private TMBiliTask handle;

	@Override
	public void onOpen(WebSocket webSocket, Response response) {
		System.out.println("WebSocketListener->>onOpen");
		socket = webSocket;

		handle.WsInit();
	}

	@Override
	public void onMessage(WebSocket webSocket, ByteString bytes) {
//		onMessage(webSocket, bytes.string(Charset.forName("UTF-8")));
		handle.WsHandle(bytes);
	}

	@Override
	public void onMessage(WebSocket webSocket, String text) {
		handle.WsHandle(text);
	}

	@Override
	public void onFailure(WebSocket webSocket, Throwable t, Response response) {
		System.out.println("WebSocketListener->>onFailure ");
		socket = null;
		t.printStackTrace();
	}
}
